local Lua__baowu = fk.CreateSkill {
    name = "Lua__baowu",
    tags = {Skill.Permanent}
}


Fk:loadTranslationTable{
  ["Lua__baowu"] = "宝物",
  [":Lua__baowu"] = "持恒技，共鸣技，一名其他角色回合开始时/回合结束时，你可以获得其最有价值的宝物（比较其体力值，手牌数，装备区的牌数，技能数。数量最多的一项：若为体力值，则获得其等量的体力上限然后掠夺其一半体力值，向上取整；若为手牌数，则掠夺其所有手牌；若为装备区的牌数，则掠夺其所有装备区的牌；若为技能数，则选择其一个技能获得，其失去该技能）",
  ["#Lua__baowu-choice"] = "请选择要掠夺的技能",
  ["#Lua__baowu-prompt"] = "宝物：你可获得 %src 最有价值的宝物（手牌，装备，技能，体力以及上限）。",
}


Lua__baowu:addEffect(fk.TurnStart, {
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(Lua__baowu.name) and target ~= player and
        (player.general == "DIEND" or player.deputyGeneral == "DIEND") then
            return (target.hp > #target:getCardIds("h") and target.hp > #target:getCardIds("e") and target.hp > #target:getSkillNameList()) or
            (#target:getCardIds("h") > target.hp and #target:getCardIds("h") > #target:getCardIds("e") and #target:getCardIds("h") > #target:getSkillNameList()) or
            (#target:getCardIds("e") > target.hp and #target:getCardIds("e") > #target:getCardIds("h") and #target:getCardIds("e") > #target:getSkillNameList()) or
            (#target:getSkillNameList() > target.hp and #target:getSkillNameList() > #target:getCardIds("h") and #target:getSkillNameList() > #target:getCardIds("e"))
        end
    end,

    on_cost =function (self, event, target, player, data)
        return player.room:askToSkillInvoke(player, {skill_name = Lua__baowu.name,prompt = "#Lua__baowu-prompt:"..target.id})

    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if target.hp > #target:getCardIds("h") and target.hp > #target:getCardIds("e") and target.hp > #target:getSkillNameList() then
            local hp_gain = math.floor(target.hp / 2)
            room:changeMaxHp(player, target.maxHp)
            if hp_gain > 0 then
                room:recover({
                    who = player,
                    num = hp_gain,
                    recoverBy = player,
                    skillName = Lua__baowu.name
                })
            end
        end
        if #target:getCardIds("h") > target.hp and #target:getCardIds("h") > #target:getCardIds("e") and #target:getCardIds("h") > #target:getSkillNameList() then
            room:obtainCard(player, target:getCardIds("h"), true)
        end
        if #target:getCardIds("e") > target.hp and #target:getCardIds("e") > #target:getCardIds("h") and #target:getCardIds("e") > #target:getSkillNameList() then
            room:obtainCard(player, target:getCardIds("e"), true)
        end
        if #target:getSkillNameList() > target.hp and #target:getSkillNameList() > #target:getCardIds("h") and #target:getSkillNameList() > #target:getCardIds("e") then
            local skills = {}
            for _, skill in ipairs(target.player_skills) do
                if not skill.attached_equip and not skill.name:startsWith("#") then
                    table.insert(skills, skill.name)
                end
            end
            if #skills > 0 then
                local skill = room:askToChoice(player, {
                    choices = skills,
                    skill_name = Lua__baowu.name,
                    prompt = "#Lua__baowu-choice"
                })
                if skill then
                    room:handleAddLoseSkills(player, skill, nil)
                    room:handleAddLoseSkills(target, "-"..skill, nil)
                end
            end
        end
    end,
})

Lua__baowu:addEffect(fk.TurnEnd, {
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(Lua__baowu.name) and target ~= player and
        (player.general == "DIEND" or player.deputyGeneral == "DIEND") then
            return (target.hp > #target:getCardIds("h") and target.hp > #target:getCardIds("e") and target.hp > #target:getSkillNameList()) or
            (#target:getCardIds("h") > target.hp and #target:getCardIds("h") > #target:getCardIds("e") and #target:getCardIds("h") > #target:getSkillNameList()) or
            (#target:getCardIds("e") > target.hp and #target:getCardIds("e") > #target:getCardIds("h") and #target:getCardIds("e") > #target:getSkillNameList()) or
            (#target:getSkillNameList() > target.hp and #target:getSkillNameList() > #target:getCardIds("h") and #target:getSkillNameList() > #target:getCardIds("e"))
        end
    end,
    on_cost =function (self, event, target, player, data)
        return player.room:askToSkillInvoke(player, {skill_name = Lua__baowu.name,prompt = "#Lua__baowu-prompt:"..target.id})

    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if target.hp > #target:getCardIds("h") and target.hp > #target:getCardIds("e") and target.hp > #target:getSkillNameList() then
            local hp_gain = math.floor(target.hp / 2)
            room:changeMaxHp(player, target.maxHp)
            if hp_gain > 0 then
                room:recover({
                    who = player,
                    num = hp_gain,
                    recoverBy = player,
                    skillName = Lua__baowu.name
                })
            end
        end
        if #target:getCardIds("h") > target.hp and #target:getCardIds("h") > #target:getCardIds("e") and #target:getCardIds("h") > #target:getSkillNameList() then
            room:obtainCard(player, target:getCardIds("h"), true)
        end
        if #target:getCardIds("e") > target.hp and #target:getCardIds("e") > #target:getCardIds("h") and #target:getCardIds("e") > #target:getSkillNameList() then
            room:obtainCard(player, target:getCardIds("e"), true)
        end
        if #target:getSkillNameList() > target.hp and #target:getSkillNameList() > #target:getCardIds("h") and #target:getSkillNameList() > #target:getCardIds("e") then
            local skills = {}
            for _, skill in ipairs(target.player_skills) do
                if not skill.attached_equip and not skill.name:startsWith("#") then
                    table.insert(skills, skill.name)
                end
            end
            if #skills > 0 then
                local skill = room:askToChoice(player, {
                    choices = skills,
                    skill_name = Lua__baowu.name,
                    prompt = "#Lua__baowu-choice"
                })
                if skill then
                    room:handleAddLoseSkills(player, skill, nil)
                    room:handleAddLoseSkills(target, "-"..skill, nil)
                end
            end
        end
    end,
})

return Lua__baowu 